﻿using Microsoft.Win32;
using Microsoft.XmlDiffPatch;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace DocumentorUI
{
    /// <summary>
    /// Interaction logic for XMLCompareUC.xaml
    /// </summary>
    public partial class XMLCompareUC : UserControl
    {
        private string fileOpen1;
        private string fileOpen2;
        public XMLCompareUC()
        {
            InitializeComponent();
        }
        private void wzDocGen_Finish(object sender, RoutedEventArgs e)
        {
            wzDocGen.CurrentPage = Terminated;
        }
        private void wzDocGen_PageChanged(object sender, RoutedEventArgs e)
        {
            if (wzDocGen.CurrentPage == Terminated)
            {
                System.Threading.ThreadStart ts = new System.Threading.ThreadStart(StartDocumentGeneration);
                System.Threading.Thread th = new System.Threading.Thread(ts);
                th.Start();

            }
        }
        private void btnFile1_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog sfd = new OpenFileDialog();
            sfd.DefaultExt = ".xml";
            sfd.Filter = "Document Word (.xml)|*.xml";
            if (sfd.ShowDialog() == true)
            {
                fileOpen1 = sfd.FileName;
            }
        }

        private void btnFile2_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog sfd = new OpenFileDialog();
            sfd.DefaultExt = ".xml";
            sfd.Filter = "Document Word (.xml)|*.xml";
            if (sfd.ShowDialog() == true)
            {
                fileOpen2 = sfd.FileName;
            }
        }

        private void StartDocumentGeneration()
        {
            bool fragments = false;
            XmlDiffAlgorithm algorithm = XmlDiffAlgorithm.Auto;
            XmlDiffOptions options = XmlDiffOptions.None;

            options |= XmlDiffOptions.IgnoreChildOrder;
            options |= XmlDiffOptions.IgnoreComments;
            options |= XmlDiffOptions.IgnorePI;
            options |= XmlDiffOptions.IgnoreWhitespace;
            options |= XmlDiffOptions.IgnoreNamespaces;
            options |= XmlDiffOptions.IgnorePrefixes;
            options |= XmlDiffOptions.IgnoreXmlDecl;
            options |= XmlDiffOptions.IgnoreDtd;
            fragments = true;
            algorithm = XmlDiffAlgorithm.Fast;

            // extract names from command line
            string sourceXmlFileName = fileOpen1;
            string changedXmlFileName = fileOpen2;

            MemoryStream ms = new MemoryStream();
            XmlTextWriter diffgramWriter = new XmlTextWriter(ms, Encoding.Unicode);
            //if (diffgramFileName != null)
            //{
            //    diffgramWriter = new XmlTextWriter(diffgramFileName, Encoding.Unicode);
            //}

            // create XmlDiff object & set the desired options and algorithm
            XmlDiff xmlDiff = new XmlDiff(options);
            xmlDiff.Algorithm = algorithm;

            // Compare the XML files
            bool bEqual = false;
            bEqual = xmlDiff.Compare(sourceXmlFileName, changedXmlFileName, fragments, diffgramWriter);
            ms.Position = 0;
            var sr = new StreamReader(ms);
            var myStr = sr.ReadToEnd();
            this.Dispatcher.Invoke(() =>
            {
                wzDocGen.CurrentPage = EndWizard;
                txtDiffXml.Text = myStr;
            });

        }
    }
}
